package com.lzq.chatroom_project.service.impl;

import com.lzq.chatroom_project.service.BitmapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

@Service
public class BitmapServiceImpl implements BitmapService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void markMessagePosted(Integer sessionId, Integer userId, LocalDate date) {
        int dayIndex = date.getDayOfMonth() - 1;
        redisTemplate.opsForValue().setBit("msg_bitmap:" + sessionId + "_" + userId + ":" + date.getYear() + date.getMonthValue(), dayIndex, true);
    }

    @Override
    public boolean hasMessagesOnDate(Integer sessionId, Integer userId, LocalDate date) {
        int dayIndex = date.getDayOfMonth() - 1;
        Boolean bit = redisTemplate.opsForValue().getBit("msg_bitmap:" + sessionId + "_" + userId + ":" + date.getYear() + date.getMonthValue(), dayIndex);
        return Boolean.TRUE.equals(bit);
    }
}
